package com.itheima.dao;

import com.itheima.domain.Fruit;
import com.itheima.domain.User;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    DecimalFormat df=new DecimalFormat("0.00");
    private ManagerDao managerDao = new ManagerDao();
    public void checkOut(double finalAllPrice, String id, String fruitname, int number) {
      ArrayList<User> list = new ArrayList<>();
        ArrayList<Fruit> list1 = new ArrayList<>();
        //将修改后数据存入

        //user的修改
        BufferedReader br=null;
        try {
            br = new BufferedReader(new FileReader("src\\com\\itheima\\info\\UserInfo.txt"));
            String s;
            while((s=br.readLine())!=null){
                String[] split = s.split(",");
                if(split[0].equals(id)){
                    if ((Double.parseDouble(split[2])-finalAllPrice)>=0) {
                        split[2] = "" + (Double.parseDouble(split[2]) - finalAllPrice);
                    }
                    else {
                        System.out.println("余额不足");
                        return;
                    }
                }
                User user = new User(split[0],split[1],Double.parseDouble(split[2]),Integer.parseInt(split[3]),split[4]);
                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedWriter bw=null;
        try {
            bw= new BufferedWriter(new FileWriter("src\\com\\itheima\\info\\UserInfo.txt"));
            for (int i = 0; i < list.size(); i++) {
                bw.write(list.get(i).getId()+","+list.get(i).getName()+","+list.get(i).getBalance()+","+list.get(i).getPoints()+","+list.get(i).getPassword());
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



        //fruit 的修改



        BufferedReader br1 = null;
        try {
            String s;
            br1 = new BufferedReader(new FileReader("src\\com\\itheima\\info\\FruitInfo.txt"));
            while ((s = br1.readLine()) != null) {



                String[] split = s.split(" ");
               if (split[1].equals(fruitname)){
                   int mid=Integer.parseInt(split[3])-number;
                   split[3]=""+mid;
               }

                Fruit fruit = new Fruit(split[0],split[1],Double.parseDouble(split[2]),Integer.parseInt(split[3]));
               list1.add(fruit);
            }
        }
            catch(Exception e){
            e.printStackTrace();
            }


        BufferedWriter bw1=null;
        try {
            bw1= new BufferedWriter(new FileWriter("src\\com\\itheima\\info\\FruitInfo.txt"));
            for (int i = 0; i < list1.size(); i++) {
                bw1.write(list1.get(i).getId()+" "+list1.get(i).getName()+" "+list1.get(i).getPrice()+" "+list1.get(i).getAmount());
               bw1.newLine();
                bw1.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                bw1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        }



    public Double buyFruit(String fruitname,int number) {
        Double allPrice=0d;

        ArrayList<Fruit> list = selectFruit();
        for (int i = 0; i < list.size(); i++) {
            Fruit fruit = list.get(i);
            if (fruit.getName().equals(fruitname)&&(fruit.getAmount()>0)) {
                double price = fruit.getPrice() * number;
                allPrice += price;
            }

        }

        return allPrice;
    }

    public ArrayList<Fruit> selectFruit() {
        ArrayList<Fruit> list = new ArrayList<>();

        BufferedReader br = null;
        try {
            String s;
            br = new BufferedReader(new FileReader("src\\com\\itheima\\info\\FruitInfo.txt"));
            while ((s = br.readLine()) != null) {



                String[] split = s.split(" ");
                Fruit fruit = new Fruit(split[0],split[1],Double.parseDouble(split[2]),Integer.parseInt(split[3]));
                list.add(fruit);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;

    }

    public boolean userAdmain(String id, String password) {

        boolean flag = false;
        List<User> list = managerDao.selectAll();
//        BufferedReader br = null;
//        ArrayList<User> list = new ArrayList<>();
//
//        try {
//            br = new BufferedReader(new FileReader("src\\com\\itheima\\info\\UserInfo.txt"));
//
//            String s;
//            while ((s = br.readLine()) != null) {
//                String[] split = s.split(",");
//
//                User user = new User();
//                user.setId(split[0]);
//                user.setName(split[1]);
//                user.setBalance(Double.parseDouble(split[2]));
//                user.setPoints(Integer.parseInt(split[3]));
//                user.setPassword(split[4]);
//                list.add(user);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                br.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id) && list.get(i).getPassword().equals(password)) {
                flag = true;
                break;
            }
        }

        return flag;

    }

    public boolean haveFruit(String fruitname, int number) {
        boolean flag =false;
        BufferedReader br2;
        try {
            String s;
            br2 = new BufferedReader(new FileReader("src\\com\\itheima\\info\\FruitInfo.txt"));
            while ((s = br2.readLine()) != null) {



                String[] split = s.split(" ");
                if (split[1].equals(fruitname)){
                   if ((Integer.parseInt(split[3])-number)<0){
                       flag =true;

                   }
                }


            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }
}
